<?
include "libhtml.php";
include "libsession.php";
requireLogin();
echoHeader("Edit Document");

// get the id from the params
if (isset($_GET["templateid"]))
{
	$templateid = $_GET["templateid"];	
}
else
{
	$templateid = 0;	
}

if ($templateid > 0)
{
	$sql = "SELECT Id, Name FROM WorkflowTemplate WHERE Id=$templateid;";
	
	$templates = query_db($sql);	
	$template = mysql_fetch_array($templates, MYSQL_ASSOC);
	mysql_free_result($templates);
}

// get a list of users for the actions
$sql = "SELECT Id, Name FROM User";
$users_list = query_db($sql);

// get a list of steps for the actions
$sql = "SELECT Id, Name FROM WorkflowStep WHERE TemplateId=$templateid";
$steps_list = query_db($sql);

function writeEventTable($stepid)
{
	echo "		<table width=100% border=1>";
	echo "			<tr>";
	echo "				<th>Event Type</th>";
	echo "				<th>Event Date</th>";
	echo "				<th>Event Update</th>";
	echo "			</tr>";
	
	$sql = "SELECT Id, Type, ParamDate FROM WorkflowEvent
			WHERE StepId=$stepid";

	$events = query_db($sql);
	while ($event = mysql_fetch_array($events, MYSQL_ASSOC))
	{
		writeEventRow($event);
	}
	
	
	// close the Events table
	echo "		</table>";
}

// write a single row for an event
function writeEventRow($event)
{
	global $templateid;
	global $users_list;
	global $steps_list;
						
	$isSelected_StepStart = "";
	$isSelected_Click = "";
	$isSelected_DatePast = "";
	
	if ($event["Type"] == WF_EVENT_TYPE_StepStart)
		$isSelected_StepStart = " selected='selected'";
	else if ($event["Type"] == WF_EVENT_TYPE_Click)
		$isSelected_Click = " selected='selected'";
	else if ($event["Type"] == WF_EVENT_TYPE_DatePast)
		$isSelected_DatePast = " selected='selected'";
	else
		$typetext = "Not Set";
	
	echo "<tr>\n";
	
	echo "	<form action=WorkflowUpdate.php method=post>\n";
	echo "		<input type=hidden name=action value='updateevent'>\n";
	echo "		<input type=hidden name=targetid value='" . $event["Id"] . "'>\n";
	echo "		<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>\n";
	echo "		<td>\n";
	echo "			<select name=Type>\n";
	echo "				<option value=" . WF_EVENT_TYPE_StepStart . $isSelected_StepStart . ">Step Start</option>\n";
	echo "				<option value=" . WF_EVENT_TYPE_Click . $isSelected_Click . ">Click</option>\n";
	echo "				<option value=" . WF_EVENT_TYPE_DatePast . $isSelected_DatePast . ">Date Past</option>\n";
	echo "			</select>\n";
	echo "		</td>\n";
	echo "		<td><input type=text name=ParamDate value='" . $event["ParamDate"] . "'></td>\n";
	echo "		<td>";
	echo "			<input type=image SRC='images/green-checkmark.gif' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Update Event'>";
	echo "	</form>\n";
	
	// create a new form for delete
	echo "	<form action=WorkflowUpdate.php style='display: inline;' method=post>\n";
	echo "		<input type=hidden name=action value='deleteevent'>\n";
	echo "		<input type=hidden name=targetid value='" . $event["Id"] . "'>\n";
	echo "		<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>\n";
	echo "		<input type=image SRC='images/red-x.jpg' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Delete Event'>";
	echo "	</form>";
	
	// close the row
	echo "		</td>";
	echo "</tr>\n";
	
	// build the actions table

	echo "<tr>\n";
	echo "	<td>&nbsp;</td>";
	echo "	<td colspan=3>\n";
	echo "		<h1>Actions</h1>";

	writeActionTable($event["Id"]);
	
	// write the Add Action button
	echo "		<div style='text-align: center;'>";
	echo "			<form action='WorkflowUpdate.php' method=post>";
	echo "				<input type=hidden name='action' value='addaction'>";
	echo "				<input type=hidden name=targetid value=" . $event["Id"] . ">";
	echo "				<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>";
	echo "				<input type=image SRC='images/blue-plus.jpg' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Add Action'>";
	echo "			</form>";
	echo "		</div>";
	
	echo "	</td>";
	echo "</tr>";				
						
					
}

function writeActionTable($stepid)
{
	echo "		<table>\n";
	echo "			<tr>\n";
	echo "				<th>Event Type</th>\n";
	echo "				<th>Event Date</th>\n";
	echo "				<th>Action Type</th>\n";
	echo "				<th>Action Params</th>\n";
	//echo "				<th>User</th>\n";
	//echo "				<th>Email Msg</th>\n";
	echo "				<th>Update</th>\n";
	echo "			</tr>\n";

	// write each individual action098yfgy0
	$sql = "SELECT Id, StepId, EventType, EventParamDate, Type, ParamStepId, ParamUserId, ParamEmailMsg 
			FROM WorkflowAction WHERE StepId=$stepid";

	$actions = query_db($sql);
	
	while ($action = mysql_fetch_array($actions, MYSQL_ASSOC))
	{
		writeActionRow($action);
	}
	
	// close the actions table
	echo "		</table>";
}

// write a single row for an action
function writeActionRow($action)
{
	global $templateid;
	global $steps_list;
	global $users_list;
	
	// build the option list for event type
	$isSelected_StepStart = "";
	$isSelected_Click = "";
	$isSelected_DatePast = "";
	
	if ($action["EventType"] == WF_EVENT_TYPE_StepStart)
		$isSelected_StepStart = " selected='selected'";
	else if ($action["EventType"] == WF_EVENT_TYPE_Click)
		$isSelected_Click = " selected='selected'";
	else if ($action["EventType"] == WF_EVENT_TYPE_DatePast)
		$isSelected_DatePast = " selected='selected'";
	else
		$typetext = "Not Set";
	
	// build the option list for action type
	$isSelected_ChangeOwner = "";
	$isSelected_SendEmail = "";
	$isSelected_Navigate = "";
								
	if ($action["Type"] == WF_ACTION_TYPE_ChangeOwner)
		$isSelected_ChangeOwner = " selected='selected'";
	else if ($action["Type"] == WF_ACTION_TYPE_SendEmail)
		$isSelected_SendEmail = " selected='selected'";
	else if ($action["Type"] == WF_ACTION_TYPE_Navigate)
		$isSelected_Navigate = " selected='selected'";
		
	echo "<tr>";
	echo "	<form action=WorkflowUpdate.php style='display: inline;' method=post>\n";
	echo "	<input type=hidden name=action value='updateaction'>\n";
	echo "	<input type=hidden name=targetid value='" . $action["Id"] . "'>\n";
	echo "	<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>\n";
	echo "	<td>";
	echo "		<select name=EventType>\n";
	echo "			<option value=" . WF_EVENT_TYPE_StepStart . $isSelected_StepStart . ">Step Start</option>\n";
	echo "			<option value=" . WF_EVENT_TYPE_Click . $isSelected_Click . ">Click</option>\n";
	echo "			<option value=" . WF_EVENT_TYPE_DatePast . $isSelected_DatePast . ">Date Past</option>\n";
	echo "		</select>\n";
	echo "	</td>";
	echo "	<td align=center>";
	
	if ($action["EventType"] == WF_EVENT_TYPE_DatePast)
	{
		// only show the date text box if applicable
		echo "  Doc Date + <br>";
		echo "	<input type=text name=EventParamDate value='" . $action["EventParamDate"] . "' size=2>";
		echo "  <br>days";
	}
	else
	{
		echo "N/A";	
	}
	
	echo "	</td>\n";
	echo "	<td>";
	echo "		<select name='Type'>";
	echo "			<option value=0></option>";
	echo "			<option value=" . WF_ACTION_TYPE_ChangeOwner . $isSelected_ChangeOwner . ">Change Owner</option>";
	echo "			<option value=" . WF_ACTION_TYPE_SendEmail . $isSelected_SendEmail . ">Send Email</option>";
	echo "			<option value=" . WF_ACTION_TYPE_Navigate . $isSelected_Navigate . ">Change Steps</option>";
	echo "		</select>";
	echo "	</td>";
	echo "	<td>";
	
	if ($action["Type"] == WF_ACTION_TYPE_Navigate)
	{
		// show the destination step parameter
		echo "		Destination Step:<br>";
		echo "		<select name='ParamStepId'>";
		echo "			<option value=0></option>";
		
		// reset our step list
		mysql_data_seek($steps_list, 0);
		while ($step_list = mysql_fetch_array($steps_list, MYSQL_ASSOC))
		{
			$isSelected = "";
			if ($step_list["Id"] == $action["ParamStepId"])
				$isSelected = " selected='selected'";
			echo "		<option value=" . $step_list["Id"] . $isSelected . ">" . $step_list["Name"] . "</option>";	
		}
		
		echo "		</select>";
	}
	
	if ($action["Type"] == WF_ACTION_TYPE_ChangeOwner ||
		$action["Type"] == WF_ACTION_TYPE_SendEmail)
	{
		echo "		User:<br>";
		echo "		<select name='ParamUserId'>";
		echo "			<option value=0></option>";
		
		// reset our user list
		mysql_data_seek($users_list, 0);
		
	  	while ($user=mysql_fetch_array($users_list, MYSQL_ASSOC))
	  	{
	      	$isSelected = "";
	      	if ($user["Id"] == $action["ParamUserId"])
	      	{
		      	$isSelected = '  selected="selected"';
	      	}
	      	echo "<option value=" . $user["Id"] . $isSelected . ">" . $user["Name"] . "</option>";
	  	}
	  	echo "  	</select>";
	}
  	
	if ($action["Type"] == WF_ACTION_TYPE_SendEmail)
	{
		// show the email message param
  		echo "	<br>Email Message:<br>";
		echo "	<input type=text name=ParamEmailMsg value='" . $action["ParamEmailMsg"] . "'>";
	}
	
	echo "	</td>";
	echo "	<td>";
	echo "		<input type=image SRC='images/green-checkmark.gif' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Update Action'>";
	echo "	</form>";
	
	// create a form to handle the delete button
	echo "	<form action=WorkflowUpdate.php style='display: inline;' method=post>\n";
	echo "		<input type=hidden name=action value='deleteaction'>\n";
	echo "		<input type=hidden name=targetid value='" . $action["Id"] . "'>\n";
	echo "		<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>\n";
	echo "		<input type=image SRC='images/red-x.jpg' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Delete Action'>";
	echo "	</form>";
	
	// close the table
	echo "	</td>";
	echo "</tr>";	
}
?>

<h1>Edit Workflow Template</h1>
<br><br>
<form action='WorkflowTemplate_Edit.php?templateid=<? echo $templateid; ?>' method='post'>
	<table>
		<tr>
			<td> Name: </td>
			<td><input type='text' size='100' name="Name" value='<? echo $template["Name"]?>' ></td>
		</tr>
	</table>
</form>
	<br><br><br>
	<h1>Steps</h1>
	<table>
		<tr>
			<th>Name</th>
			<th>IsLocked</th>
			<th># Documents in step</th>
			<th>Update</th>
		</tr>
		<?
			// get the workflow steps associated with this template
			$sql = "SELECT Id, Name, IsLocked,
						(SELECT COUNT(1) FROM Document WHERE WorkflowStepId=ws.Id) AS Occurs
					FROM WorkflowStep ws
					WHERE TemplateId=$templateid";
			
			$steps = query_db($sql);
			
			while ($step = mysql_fetch_array($steps, MYSQL_ASSOC))
			{
				$ischecked = " ";
				if ($step["IsLocked"] == 1)
					$ischecked = " checked='checked' ";
				
				//$javascript = 'document.getElementById("chklocked").value = document.getElementById("chklocked").checked;';
				echo "<tr>";
				echo "	<form action=WorkflowUpdate.php method=post>\n";
				echo "		<input type=hidden name=action value='updatestep'>\n";
				echo "		<input type=hidden name=targetid value='" . $step["Id"] . "'>\n";
				echo "		<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>\n";
				echo "		<td><input type=text name='Name' value='" . $step["Name"] . "'></td>";
				echo "		<td align=center><input type=checkbox id='chklocked' name='IsLocked' value=locked " . $ischecked ."></td>";
				echo "		<td align=center>" . $step["Occurs"] . "</td>";
				echo "		<td>";
				echo "			<input type=image SRC='images/green-checkmark.gif' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Update Step'>";
				echo "	</form>\n";
				
				// add a new form to delete the step
				if ($step["Occurs"] == 0)
				{
					echo "			<form action=WorkflowUpdate.php style='display: inline;' method=post>\n";
					echo "				<input type=hidden name=action value='deletestep'>\n";
					echo "				<input type=hidden name=targetid value='" . $step["Id"] . "'>\n";
					echo "				<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>\n";
					echo "				<input type=image SRC='images/red-x.jpg' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Delete Step'>";
					echo "			</form>";
				}
				echo "		</td>\n";
				echo "</tr>";
				
				// get the actions
				
				
				echo "<tr>";
				echo "	<td>&nbsp;</td>";
				echo "	<td colspan=3>";
				echo "		<h1>Actions</h1>";
				
				writeActionTable($step["Id"]);

				// write the add action button
				echo "<br>";
				echo "		<div style='text-align: center;'>";
				echo "			<form action='WorkflowUpdate.php' method=post>";
				echo "				<input type=hidden name='action' value='addaction'>";
				echo "				<input type=hidden name=targetid value=" . $step["Id"] . ">";
				echo "				<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=" . $templateid . "'>";
				echo "				<input type=image SRC='images/blue-plus.jpg' HEIGHT='30' WIDTH='30' BORDER='0'>Add Action";
				echo "			</form>";
				echo "		</div>";
				
				echo "	</td>";
				echo "</tr>";
				
			
			}
			
		?>
	</table>
	<div style='text-align: center;'>
		<form action="WorkflowUpdate.php" method=post>
			<input type=hidden name=action value='addstep'>
			<input type=hidden name=targetid value='<? echo $templateid ?>'>
			<input type=hidden name=redirecturl value='WorkflowTemplate_Edit.php?templateid=<? echo $templateid ?>'>
			<input type=image SRC='images/blue-plus.jpg' HEIGHT='30' WIDTH='30' BORDER='0'>Add Step
		</form>
	</div>

<? echoFooter(); ?>